<?php

/**
 * LayoutBehavior class
 *
 * @author Julot <andy.julot@gmail.com>
 * @since Monday, January 30, 2012. 09:21 AM
 */
class LayoutBehavior extends CBehavior {

	public function getMenu() {
		static $menu = null;

		if (!isset($menu)) {
			$user = Yii::app()->user;
			$module = Yii::app()->getModule('user');
			$menu = array(
				array(
					'label'=>'Home',
					'url'=>array('/site/index')
				),
				array(
					'label'=>'Upload',
					'url'=>array('/language/create'),
				),
				array(
					'url'=>$module->loginUrl,
					'label'=>$module->t("Login"),
					'visible'=>$user->isGuest
				),
				array(
					'url'=>$module->registrationUrl,
					'label'=>$module->t("Register"),
					'visible'=>$user->isGuest
				),
				array(
					'url'=>$module->profileUrl,
					'label'=>$module->t("Profile"),
					'visible'=>!$user->isGuest
				),
				array(
					'url'=>$module->logoutUrl,
					'label'=>$module->t("Logout") . ' (' . $user->name . ')',
					'visible'=>!$user->isGuest
				),
			);
		}

		return $menu;
	}

	public function getMangaMenu() {
		static $mangaMenu = null;

		if (!isset($mangaMenu)) {
			$mangaMenu = array();
			$criteria = new CDbCriteria();
			$criteria->order = 'title';
			$models = Manga::model()->findAll($criteria);
			foreach ($models as $model) {
				$mangaMenu[] = array(
					'label'=>$model->title,
					'url'=>array('/manga/view', 'id'=>$model->id),
				);
			}
		}

		return $mangaMenu;
	}

	public function getChapterMenu() {
		static $chapterMenu = null;

		if (!isset($chapterMenu)) {
			$chapterMenu = array();
			$selects = array(
				'c.id',
				'c.sequence',
				'c.subSequence',
				'c.title as chapterTitle',
				'm.title as mangaTitle',
			);
			$models = Yii::app()->db->createCommand()
				->select(implode(', ', $selects))
				->from('chapters c')
				->join('mangas m', 'c.mangaId = m.id')
				->order('m.title, c.sequence, c.subSequence, c.title')
				->queryAll();
			foreach ($models as $model) {
				$sequence = $model['sequence'] . $model['subSequence'];
				$title = $model['chapterTitle'];
				if ($sequence && $title) {
					$chapter = $sequence . '. ' . $title;
				} else if ($sequence) {
					$chapter = 'Chapter ' . $sequence;
				} else {
					$chapter = $title;
				}
				$label = $model['mangaTitle'] . ' - ' . $chapter;
				$chapterMenu[] = array(
					'label'=>$label,
					'url'=>array('/chapter/view', 'id'=>$model['id']),
				);
			}
		}

		return $chapterMenu;
	}

	public function getAdminMenu() {
		static $adminMenu = null;

		if (!isset($adminMenu)) {
			$adminMenu = array(
				array(
					'label'=>'Create 4-Koma',
					'url'=>array('/manga/create')
				),
				array(
					'label'=>'Create Chapter',
					'url'=>array('/chapter/create'),
				),
			);
		}

		return $adminMenu;
	}

	private $_assetUrl;

	public function getAssetUrl() {
		if ($this->_assetUrl === null) {
			$theme = $this->owner->theme;
			$path = Yii::getPathOfAlias("application.assets.$theme->name");
			$this->_assetUrl = Yii::app()->assetManager->publish(
				$path, false, -1, YII_DEBUG
			);
		}
		return $this->_assetUrl;
	}
	
}
